package com.mk.jta.xa.demo.service;

import com.mk.jta.xa.demo.primary.dao.UserRepository;
import com.mk.jta.xa.demo.primary.entity.User;
import com.mk.jta.xa.demo.secondary.dao.OrderRepository;
import com.mk.jta.xa.demo.secondary.entity.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class JtaService {

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private OrderRepository orderRepository;

    @Transactional
    public void save() {
        User user = new User(1L, "lxm");
        Order order = new Order(1L, "001");
        User u1 = userRepository.save(user);
        Order s1 = orderRepository.save(order);
    }

    @Transactional
    public void saveRollBack() {
        User user = new User(2L, "lxm");
        Order order = new Order(2L, "001");
        User u1 = userRepository.save(user);
        Order s1 = orderRepository.save(order);
        int i = 10 / 0;
    }

    @Transactional
    public void update() {
        User user = userRepository.getById(1L);
        user.setName("hhh");
        Order order = orderRepository.getById(1L);
        order.setOrderId("666");
        User u1 = userRepository.save(user);
        Order s1 = orderRepository.save(order);
    }
}
